What is buffer-from?
The buffer-from npm package is a polyfill for the `Buffer.from` method that was added in Node.js v5.10.0. It allows users to create `Buffer` objects from various types of data such as strings, arrays, or other buffers. This package is particularly useful for environments that do not support the newer `Buffer.from` API.
What are buffer-from's main functionalities?
Creating a buffer from a string
This feature allows you to create a buffer from a string. The second argument specifies the string encoding.
const bufferFrom = require('buffer-from');
const buffer = bufferFrom('hello world', 'utf8');
Creating a buffer from an array
This feature allows you to create a buffer from an array of bytes.
const bufferFrom = require('buffer-from');
const buffer = bufferFrom([0x68, 0x65, 0x6c, 0x6c, 0x6f]);
Creating a buffer from another buffer
This feature allows you to create a new buffer from an existing buffer. The new buffer will be a copy of the original.
const bufferFrom = require('buffer-from');
const buffer1 = Buffer.alloc(10).fill('a');
const buffer2 = bufferFrom(buffer1);
Other packages similar to buffer-from
safe-buffer
The safe-buffer package provides a safer way of using Node.js buffers by ensuring compatibility with older versions of Node.js. It offers a similar API to buffer-from, but it also includes additional methods for buffer allocation and conversion.
buffer-alloc
The buffer-alloc package is specifically designed to create new buffer instances in a way that is safe across various Node.js versions. It is similar to buffer-from in that it provides a polyfill for buffer creation, but it focuses on allocation rather than conversion from other data types.
Buffer From
A ponyfill for Buffer.from
, uses native implementation if available.
Installation
npm install --save buffer-from
Usage
const bufferFrom = require('buffer-from')
console.log(bufferFrom([1, 2, 3, 4]))
const arr = new Uint8Array([1, 2, 3, 4])
console.log(bufferFrom(arr.buffer, 1, 2))
console.log(bufferFrom('test', 'utf8'))
const buf = bufferFrom('test')
console.log(bufferFrom(buf))
API
bufferFrom(array)
Allocates a new Buffer
using an array
of octets.
bufferFrom(arrayBuffer[, byteOffset[, length]])
arrayBuffer
<ArrayBuffer> The .buffer
property of a TypedArray or ArrayBufferbyteOffset
<Integer> Where to start copying from arrayBuffer
. Default: 0
length
<Integer> How many bytes to copy from arrayBuffer
. Default: arrayBuffer.length - byteOffset
When passed a reference to the .buffer
property of a TypedArray instance, the
newly created Buffer
will share the same allocated memory as the TypedArray.
The optional byteOffset
and length
arguments specify a memory range within
the arrayBuffer
that will be shared by the Buffer
.
bufferFrom(buffer)
buffer
<Buffer> An existing Buffer
to copy data from
Copies the passed buffer
data onto a new Buffer
instance.
bufferFrom(string[, encoding])
string
<String> A string to encode.encoding
<String> The encoding of string
. Default: 'utf8'
Creates a new Buffer
containing the given JavaScript string string
. If
provided, the encoding
parameter identifies the character encoding of
string
.
See also